<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

use App\Models\StaffOrder;
use App\Models\CarAuth;
use App\Services\CarAuthServices;

class StaffCarAuth extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'staff:car-auth';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '员工车批量授权';

    protected $mobile;

    /**
     * Create a new command instance.
     *
     * @return void
     */
    function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $orders = StaffOrder::where('status', 1)
                            ->orderBy('id', 'desc')
                            ->get();
        // send sms
        $carAuth = new CarAuthServices;

        // orders
        $n = 0;
        foreach ($orders as $order) {
            if (CarAuth::where('vpl_number', $order->vpl_number)->count() > 0) {
                $this->info('EXIST: ' . $order->vpl_number);
                continue;
            }

            $this->info(sprintf('NEW: %04d. %s', ++$n, $order->vpl_number));

            // delete old auths
            // $carAuth->deleteByVpl( $order->vpl_number );
            // sleep(2);
            $carAuth->issueAuth( $order );
            // return ;
            sleep(5);
        }

    }

}
